<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ktmall:skuAttrValues:add']"
          >新增关联</el-button
        >
        
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ktmall:skuAttrValues:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="skuAttrValuesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="SKU编码" align="center" prop="stockSku" />
      <el-table-column label="属性名" align="center" prop="attrName" />
      <el-table-column label="属性值" align="center" prop="attrValue" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ktmall:skuAttrValues:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加SKU关联属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="SKU编码" prop="stockSku">
          <el-input v-model="form.stockSku" disabled />
        </el-form-item>

        <el-form-item label="属性" prop="attrName">
          <el-select
            v-model="form.attrName"
            placeholder="请选择属性"
            clearable
            @change="fetchOptionsValue"
          >
            <el-option
              v-for="dict in attrNameOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="属性值" prop="attrValueId">
          <el-select
            v-model="form.attrValueId"
            placeholder="请选择属性值"
            clearable
          >
            <el-option
              v-for="dict in attrValueOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSkuAttrValues,
  delSkuAttrValues,
  addSkuAttrValues,
  getAttrName,
  getAttrValues,
} from "@/api/ktmall/skuAttrValues";

export default {
  name: "SkuAttrValues",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      //属性下拉框
      attrNameOptions: [],
      //属性值下拉框
      attrValueOptions: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // SKU关联属性表格数据
      skuAttrValuesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockSku: null,
      },
      // 表单参数
      form: {},
      rules: {
        stockSku: [
          { required: true, message: "sku编码不能为空", trigger: "change" },
        ],
        attrName: [
          { required: true, message: "请选择属性", trigger: "change" },
        ],
        attrValueId: [
          { required: true, message: "请选择属性值", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.queryParams.stockSku = this.$route.query.stockSku;
    this.getList();
  },
  methods: {
    /**获取属性值下拉框 */
    fetchOptionsValue() {
      getAttrValues(this.form.attrName, this.queryParams.stockSku).then(
        (response) => {
          this.attrValueOptions = response.rows;
        }
      );
    },

    /**获取属性下拉框 */
    fetchOptionsName() {
      getAttrName(this.queryParams.stockSku).then((response) => {
        this.attrNameOptions = response.rows;
      });
    },

    /** 查询SKU关联属性列表 */
    getList() {
      this.loading = true;
      listSkuAttrValues(this.queryParams).then((response) => {
        this.skuAttrValuesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockSku: null,
        attrName: null,
        attrValueId: null,
        status: null,
        remark: null,
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建关联属性";
      this.form.stockSku = this.queryParams.stockSku;
      this.fetchOptionsName();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addSkuAttrValues(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除SKU关联属性编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSkuAttrValues(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
